<template>
  <div class="pages-article-detail">
    <div :style="{ background: `${bgColor}` }" class="cover">
      <div class="content">
        <div class="inner">
          <div class="logo">
            <img :src="projectDetail.cover" />
          </div>

          <div class="info">
            <p>{{ projectDetail.name }}</p>
            <p>{{ projectDetail.introduce }}</p>
          </div>
          <div
            @click="addPraise"
            :class="['control', { active: projectDetail.isPraise == 1 }]"
          >
            <div>
              <svg-icon icon-class="like-fill" />
            </div>
            <p>
              给个支持
              <span v-if="projectDetail.praiseNum > 0">
                (
                {{
                  projectDetail.praiseNum < 10
                    ? "0" + projectDetail.praiseNum
                    : projectDetail.praiseNum
                }}
                {{ projectDetail.praiseNum > 99 ? "+" : "" }})
              </span>
            </p>
          </div>
        </div>
      </div>
    </div>

    <main>
      <div class="article">
        <layRender
          v-if="articleDetail.contentType == 2"
          :content="articleDetail.modelEditContent"
        ></layRender>

        <markdown-highlight
          v-if="articleDetail.contentType == 1"
          :value="articleDetail.htmlContent"
        ></markdown-highlight>
      </div>
      <div class="comment-list">
        <comment :targetId="articleId"></comment>
      </div>
    </main>
  </div>
</template>

<script>
import comment from "@/components/comment/comment";
import layRender from "vue2-lay-render";
import markdownHighlight from "@/components/markdown-highlight/index.vue";
import ColorThief from "colorthief";

export default {
  name: "WorkspaceJsonArticleDetail",
  components: {
    comment,
    layRender,
    markdownHighlight,
  },
  data() {
    return {
      articleId: "",
      projectId: "",
      articleDetail: {},
      articleClassify: [],
      projectDetail: {},
      bgColor: "",
    };
  },

  mounted() {
    this.articleId = this.$route.params.articleId;
    this.projectId = this.$route.params.projectId;

    this.getArticleDetail();
    this.getProjectDetail();
  },

  computed: {
    ipValue() {
      let territory = this.articleDetail.territory || "";
      return territory.split("-")[1] || "";
    },
  },
  methods: {
    getArticleDetail() {
      this.$http("getArticleDetail", { articleId: this.articleId }).then(
        (res) => {
          this.articleDetail = res.data;
          this.articleClassify = res.data.articleClassify;
          document.title = this.articleDetail.title;
        }
      );
    },
    // 获取项目详情
    getProjectDetail() {
      this.$http("getProjectDetail", {
        projectId: this.projectId,
      }).then(async (res) => {
        let data = res.data;
        const img = new Image();
        img.crossOrigin = "Anonymous";
        img.src = data.cover;
        await new Promise((resolv, reject) => {
          img.onload = resolv;
        });
        const colorThief = new ColorThief();
        let color = colorThief.getPalette(img, 3);
        this.bgColor = `rgba(${color[0].join(",")},.9)`;

        this.projectDetail = data;
      });
    },
    // 点赞
    addPraise() {
      if (this.projectDetail.isPraise == 1) return;
      this.$http("addPraise", {
        targetId: this.projectId,
        targetType: "3",
      }).then((res) => {
        this.projectDetail.isPraise = 1;
        this.projectDetail.praiseNum += 1;
        this.$notify({
          title: "赠人玫瑰,手留余香",
          message: "你的支持是我前进的动力!",
          type: "success",
        });
      });
    },

    formatDate(d) {
      let date = new Date(d);
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      month = month < 10 ? "0" + month : month;
      let day = date.getDate();
      day = day < 10 ? "0" + day : day;
      return `${year}年${month}月${day}日`;
    },
  },
};
</script>

<style lang="scss" scoped>
.pages-article-detail {
  width: 100%;
  height: 100vh;
}

.cover {
  height: 60%;
  position: relative;
  background: #fff;
  // background-image: url("https://dongping-blog.oss-cn-hangzhou.aliyuncs.com/systemImage/48380973b3514d2ca84472bf697088d7.png");
  background-position: center center;
  background-size: cover;
  background-repeat: no-repeat;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;

  &::before {
    content: "";
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.5);
  }
  .content {
    max-width: 800px;
    width: 100%;
    position: relative;
    z-index: 6;
    background: #fff;
    color: #333;
    border-radius: 10px;
    padding: 25px;
    box-sizing: border-box;
    margin-top: 80px;
    .inner {
      display: flex;
      letter-spacing: 1.2px;
      align-items: center;
      .logo {
        width: 80px;
        height: 80px;
        margin-right: 10px;
      }
      .info {
        height: 100%;
        flex: 1;
        padding-right: 10px;
        p {
          &:nth-child(1) {
            font-size: 24px;
            font-weight: 600;
          }
          &:nth-child(2) {
            font-size: 18px;
            color: #999;
            margin-top: 8px;
          }
        }
      }
      .control {
        text-align: center;
        position: relative;
        padding: 0 20px 0 40px;
        color: #999;
        &::before {
          content: "";
          width: 1px;
          height: 100%;
          background: #f5f5f5;
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
        p {
          font-size: 12px;
          color: #999;
          margin-top: 5px;
        }
      }
      .active {
        color: rgba(46, 188, 60, 1);
        P {
          color: rgba(46, 188, 60, 1);
        }
      }
    }
  }
}

main {
  max-width: 800px;
  margin-top: -80px;
  padding: 0 10px 1px;
  margin: -80px auto 0;
  position: relative;
  z-index: 8;
  .article {
    min-height: 80vh;
    background: #fff;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.07);
    padding: 20px;
    border-radius: 6px;
  }
  .comment-list {
    background: #ffff;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.07);
    padding: 20px;
    margin: 20px 0 100px;
  }
}

::v-deep .lay-text-type-1 {
  div {
    min-width: 200px !important;
  }
}

@media (max-width: 800px) {
  .cover {
    .content {
      margin: 80px 20px 0 !important;
    }
  }
}
@media (max-width: 550px) {
  .cover {
    .content {
      padding: 10px 5px !important;
      margin: 80px 10px 0 !important;
      .logo {
        width: 50px !important;
        height: 50px !important;
      }
      .info {
        p {
          &:nth-child(1) {
            font-size: 20px !important;
          }
          &:nth-child(2) {
            font-size: 14px !important;
            margin-top: 5px;
            @include main-text-hidden(2);
          }
        }
      }
      .control {
        padding: 10px !important;
      }
    }
  }
  main {
    padding: 0;
  }
  .article {
    padding: 15px 0 !important;
  }
}
</style>
